#include "boxWindow.h"
#include <QString>
#include <String.h>
#include <qmetaobject.h>
#include <qobject.h>
#include <qpushbutton.h>
#include "ui_boxWindow.h"
#include <TopoDS_Vertex.hxx>
#include "BRep_Tool.hxx"



boxWindow::boxWindow(modelSoft* myModelSoft, CubeFeature* m_cubeFeature, QWidget *parent)
	: myModelSoft(myModelSoft)
	, cubeFeature(m_cubeFeature)
	,QWidget(parent)
	,ui(new Ui::boxWindowClass())
{
	ui->setupUi(this);
	ui->lengthInput->setText(QString::number(cubeFeature->getLength()));
	ui->widthInput->setText(QString::number(cubeFeature->getWidth()));
	ui->highInput->setText(QString::number(cubeFeature->getHigh()));
	shapeID = cubeFeature->getShapeId();
	beforeFeature->setShapeId(cubeFeature->getShapeId());
	beforeFeature->setLocation(cubeFeature->getLocation());
	beforeFeature->setWidth(cubeFeature->getWidth());
	beforeFeature->setHigh(cubeFeature->getHigh());
	beforeFeature->setLength(cubeFeature->getLength());
	//查找该Pipeline是否已存在，存在则获取，不存在则创建
	auto iter1 = myModelSoft->m_pipelineMap.find(cubeFeature->getShapeId());
	if (iter1 == myModelSoft->m_pipelineMap.end()) {
		if (myModelSoft->m_historyManager->getHistorySelect()) {
			//另建一个临时Pipeline显示基本体素
			feature->setShapeId(100);
			feature->setLocation(cubeFeature->getLocation());
            feature->setWidth(cubeFeature->getWidth());
            feature->setHigh(cubeFeature->getHigh());
            feature->setLength(cubeFeature->getLength());
			cubeTemPipeline = myModelSoft->makeBox(feature);
			cubeTemPipeline->Actor()->SetPickable(false);
			cubeTemPipeline->Actor()->GetProperty()->DeepCopy(myModelSoft->myView->getInteractorStyle()->highlightProperty);
		}
		else {
	cubePipeline = myModelSoft->makeBox(cubeFeature);
	cubePipeline->Actor()->SetPickable(false);
		}
	}
	else {
		cubePipeline = iter1->second;
		
		//已存在pipeline,高亮显示
		cubePipeline->Actor()->GetProperty()->DeepCopy(myModelSoft->myView->getInteractorStyle()->highlightProperty);
	}
	myModelSoft->myView->getRenderWindow()->Render();
	createConnections();
}

void boxWindow::receivePos(const TopoDS_Shape& shape)
{
	const TopoDS_Vertex& vertex = TopoDS::Vertex(shape);
	// 获取顶点坐标
	if (!vertex.IsNull()) {
		point_pos = BRep_Tool::Pnt(vertex);
		cubeFeature->setLocation(point_pos);
		cubePipeline = myModelSoft->updateCubePipelineShape(cubePipeline, cubeFeature);
		cubePipeline->Actor()->SetPickable(false);
	}
	else {
		qDebug() << "null vertex";
	}
}

void boxWindow::onClickVertexSelect()
{
	myModelSoft->myView->getInteractorStyle()->setSelectionMode(SM_Vertex);
	myModelSoft->changeSelector(SM_Vertex);
	myModelSoft->myView->getInteractorStyle()->setCurrSelector(myModelSoft->currSelector);
}

void boxWindow::onLengthChanged()
{
	cubeFeature->setLength(ui->lengthInput->text().toDouble());
	std::list<BaseFeature*> list = cubeFeature->GetOperationList();
	//从历史树选择，需同步修改已操作步骤
	if (myModelSoft->m_historyManager->getHistorySelect() && list.size() != 0) {
		//撤消重做时已完成递归操作了，只需对最后操作进行撤消重做即可(前面操作会递归的在撤消重做中进行)
		//获取list链尾元素
		feature->setLength(ui->lengthInput->text().toDouble());
		cubeTemPipeline = myModelSoft->updateCubePipelineShape(cubeTemPipeline, feature);
		cubeTemPipeline->Actor()->GetProperty()->DeepCopy(myModelSoft->myView->getInteractorStyle()->highlightProperty);
		BaseFeature* lastFeature = list.back();
		myModelSoft->m_historyManager->undoByFeature(lastFeature);
		myModelSoft->m_historyManager->redoByFeature(lastFeature);
		myModelSoft->myView->getRenderWindow()->Render();

		int row1 = myModelSoft->m_historyManager->historyTree->rowCount() - 1;
		myModelSoft->m_historyManager->historyTree->removeRow(row1);
	}
	else {
	cubePipeline = myModelSoft->updateCubePipelineShape(cubePipeline, cubeFeature);
	cubePipeline->Actor()->SetPickable(false);
}

}

void boxWindow::onWidthChanged()
{
	cubeFeature->setWidth(ui->widthInput->text().toDouble());
	std::list<BaseFeature*> list = cubeFeature->GetOperationList();
	//从历史树选择，需同步修改已操作步骤
	if (myModelSoft->m_historyManager->getHistorySelect() && list.size() != 0) {
		//撤消重做时已完成递归操作了，只需对最后操作进行撤消重做即可(前面操作会递归的在撤消重做中进行)
		//获取list链尾元素
		feature->setWidth(ui->widthInput->text().toDouble());
		cubeTemPipeline = myModelSoft->updateCubePipelineShape(cubeTemPipeline, feature);
		cubeTemPipeline->Actor()->GetProperty()->DeepCopy(myModelSoft->myView->getInteractorStyle()->highlightProperty);
		BaseFeature* lastFeature = list.back();
		myModelSoft->m_historyManager->undoByFeature(lastFeature);
		myModelSoft->m_historyManager->redoByFeature(lastFeature);
		myModelSoft->myView->getRenderWindow()->Render();

		int row1 = myModelSoft->m_historyManager->historyTree->rowCount() - 1;
		myModelSoft->m_historyManager->historyTree->removeRow(row1);
	}
	else {
	cubePipeline = myModelSoft->updateCubePipelineShape(cubePipeline, cubeFeature);
	cubePipeline->Actor()->SetPickable(false);
	}
	

}

void boxWindow::onHighChanged()
{
	cubeFeature->setHigh(ui->highInput->text().toDouble());
	std::list<BaseFeature*> list = cubeFeature->GetOperationList();
	//从历史树选择，需同步修改已操作步骤
	if (myModelSoft->m_historyManager->getHistorySelect() && list.size() != 0) {
		//撤消重做时已完成递归操作了，只需对最后操作进行撤消重做即可(前面操作会递归的在撤消重做中进行)
		//获取list链尾元素
		feature->setHigh(ui->highInput->text().toDouble());
		cubeTemPipeline = myModelSoft->updateCubePipelineShape(cubeTemPipeline, feature);
		cubeTemPipeline->Actor()->GetProperty()->DeepCopy(myModelSoft->myView->getInteractorStyle()->highlightProperty);
		BaseFeature* lastFeature = list.back();
		myModelSoft->m_historyManager->undoByFeature(lastFeature);
		myModelSoft->m_historyManager->redoByFeature(lastFeature);
		myModelSoft->myView->getRenderWindow()->Render();

		int row1 = myModelSoft->m_historyManager->historyTree->rowCount() - 1;
		myModelSoft->m_historyManager->historyTree->removeRow(row1);
	}
	else {
	cubePipeline = myModelSoft->updateCubePipelineShape(cubePipeline, cubeFeature);
	cubePipeline->Actor()->SetPickable(false);
	}

}

void boxWindow::createConnections() {
	
	connect(ui->confirmButton, &QPushButton::clicked, this, &boxWindow::makeBox_onClick);
	connect(myModelSoft->currVertexSelector, &OccVertexSelector::sentVertex, this, &boxWindow::receivePos);
	connect(ui->cancelButton, &QPushButton::clicked, this, &boxWindow::close);
	connect(ui->vertexSelectButton, &QPushButton::clicked, this, &boxWindow::onClickVertexSelect);
	connect(ui->lengthInput, &QLineEdit::textChanged, this, &boxWindow::onLengthChanged);
	connect(ui->widthInput, &QLineEdit::textChanged, this, &boxWindow::onWidthChanged);
	connect(ui->highInput, &QLineEdit::textChanged, this, &boxWindow::onHighChanged);
}

void boxWindow::closeEvent(QCloseEvent* event)
{
	if (!myModelSoft->m_historyManager->getHistorySelect()) {
		if (!confirmCreate) myModelSoft->RemovePipelineFromRenderer(cubePipeline);
	}
	else {
	if (!confirmCreate) {
			if (!cubeTemPipeline.IsNull()) {
				myModelSoft->RemovePipelineFromRenderer(cubeTemPipeline);
				cubeFeature->setHigh(beforeFeature->getHigh());
				cubeFeature->setLength(beforeFeature->getLength());
				cubeFeature->setWidth(beforeFeature->getWidth());
				std::list<BaseFeature*> list = cubeFeature->GetOperationList();
				BaseFeature* lastFeature = list.back();
				myModelSoft->m_historyManager->undoByFeature(lastFeature);
				myModelSoft->m_historyManager->redoByFeature(lastFeature);
			myModelSoft->myView->getRenderWindow()->Render();

				int row1 = myModelSoft->m_historyManager->historyTree->rowCount() - 1;
				myModelSoft->m_historyManager->historyTree->removeRow(row1);
			}
			else {
				cubePipeline->Actor()->GetProperty()->DeepCopy(myModelSoft->myView->getInteractorStyle()->baseProperty);

			}
		}
		myModelSoft->m_historyManager->setHistorySelect(false);
	}
	
	myModelSoft->myView->getRenderWindow()->Render();
}

double boxWindow::get_box_length() {
	QString lengthText = ui->lengthInput->text();
	return lengthText.toDouble();
}

double boxWindow::get_box_width() {
	QString widthText = ui->widthInput->text();
	 
	return widthText.toDouble();
}

double boxWindow::get_box_high() {
	QString highText = ui->highInput->text();
	return highText.toDouble();
}

void boxWindow::makeBox_onClick() {
	confirmCreate = true;
	if (myModelSoft->m_historyManager->getHistorySelect()) {
		if (!cubeTemPipeline.IsNull()) {
			myModelSoft->RemovePipelineFromRenderer(cubeTemPipeline);
		}
		else {
			cubePipeline->Actor()->GetProperty()->DeepCopy(myModelSoft->myView->getInteractorStyle()->baseProperty);
		}
	}
	else {
	cubePipeline->Actor()->SetPickable(true);
	QStandardItem* item1 = new QStandardItem(QString("立方体-%1").arg(cubePipeline->GetShapeID()));
	int row = myModelSoft->ui->model->rowCount();
	item1->setEditable(false);
	myModelSoft->ui->model->setItem(row, 0, item1);
	QStandardItem* item2 = new QStandardItem(QString("隐藏"));
	item2->setCheckable(true);
	myModelSoft->ui->model->setItem(row, 1, item2);
	
	}
	
	close();
}

boxWindow::~boxWindow()
{}
